package im430.santashelper.springmvc;

import im430.santashelper.business.Address;
import im430.santashelper.business.Child;
import im430.santashelper.dao.AddressDAO;
import im430.santashelper.dao.ChildDAO;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/addresses")
public class AddressesController {

	private AddressDAO addressDAO;
	private ChildDAO childDAO;
	
	@ModelAttribute("addresses")
	public List<Address> getAddresses(){
		return getAddressDAO().getAllAddresses();
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String listAddresses(Model model) {		
		return "addresses/list";
	}
	
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public void formCreateChild(@ModelAttribute Address address) {	
		address = new Address();
	}
	
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String createChild(Address address) {
		getAddressDAO().addAddress(address);
		return "redirect:../list";
	}
	
	@RequestMapping(value = "/update/{addressId}", method = RequestMethod.GET)
	public String formUpdateAddress(@PathVariable int addressId, @ModelAttribute Address address, Model model) {
		address = getAddressDAO().getAddressById(addressId);
		model.addAttribute("addressData", address);
		return "addresses/update";
	}
	
	@RequestMapping(value = "/update/{addressId}", method = RequestMethod.POST)
	public String updateAddress(@PathVariable int addressId, Address address) {
		Address temp = getAddressDAO().getAddressById(addressId);
		temp.setText(address.getText());
		getAddressDAO().updateAddress(temp);		
		return "redirect:../list";
	}
	
	@RequestMapping(value = "/update/{childId}/{addressId}", method = RequestMethod.GET)
	public String formUpdateAddressAndChild(@PathVariable int childId, @PathVariable int addressId, @ModelAttribute AddressFormBean addressBean, Model model) {
		
		Address address = getAddressDAO().getAddressById(addressId);
		
		addressBean = new AddressFormBean();
		addressBean.setExistingAddressIndex(address.getId());
				
		model.addAttribute("addressData", addressBean);		
		model.addAttribute("child_id", childId);
		
		return "addresses/edit";
	}
	
	@RequestMapping(value = "/update/{childId}/{addressId}", method = RequestMethod.POST)
	public String updateAddressAndChild(@PathVariable int childId, @PathVariable int addressId, AddressFormBean addressBean) {
		
		Address address = null;
		boolean invalidInput = false;
		
		if(addressBean.getText() == "" && addressBean.getExistingAddressIndex() != -1) {
			address = getAddressDAO().getAddressById(addressBean.getExistingAddressIndex());
		} else if (addressBean.getText() != ""){
			address = new Address();
			address.setText(addressBean.getText());
			getAddressDAO().addAddress(address);
		} else {
			invalidInput = true;
		}
		
		if(!invalidInput) {
			Child c = getChildDAO().getChildById(childId);	
			c.setAddress(address);
			getChildDAO().updateChild(c);
		}
		
		return "redirect:../../../children/read/" + childId;

	}
	
	@RequestMapping(value = "/delete/{addressId}", method = RequestMethod.GET)
	public String deleteAddress(@PathVariable int addressId) {
		
		List<Child> allChildren = childDAO.getAllChildren();
		
		for(Child c : allChildren) {
			if(c.getAddress().getId() == addressId) {
				// from now on, this child lives nowhere
				c.setAddress(getAddressDAO().getAddressById(1));
				getChildDAO().updateChild(c);
			}
		}
		
		getAddressDAO().removeAddress(getAddressDAO().getAddressById(addressId));
		
		return "redirect:../list";
	}
	
	public AddressDAO getAddressDAO() {
		return addressDAO;
	}

	@Autowired
	public void setAddressDAO(AddressDAO addressDAO) {
		this.addressDAO = addressDAO;
	}
	
	public ChildDAO getChildDAO() {
		return childDAO;
	}

	@Autowired
	public void setChildDAO(ChildDAO childDAO) {
		this.childDAO = childDAO;
	}
	
}
